/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package newpackage;
import org.junit.Test;
import static org.junit.Assert.*;
import newpackage.IGameLogic.Winner;

/**
 *
 * @author localadm
 */
public class TestGameFinished {
    private GameLogicTeamRoDK logic;
    private int gameLevel = 2;

   @Test
    public void testInitializeGame() {
            logic = new GameLogicTeamRoDK();
            logic.initializeGame(7, 6, 1);

            assertEquals(7, logic.getX());
            assertEquals(6, logic.getY());
            assertEquals(0, logic.getGameBoard()[0][0]);
            assertEquals(0, logic.getGameBoard()[1][1]);
           
    }
  
    @Test
     public void testGameIsFinished1() {
     testInitializeGame();
     int[][] board1 = {
         { 0, 0, 0, 0, 2,1 },
         { 0, 0, 0, 1, 2,1 },
         { 0, 0, 0, 2, 1,2 },
         { 0, 0 ,1 ,1 ,1, 2 },
         { 0, 0 ,2 ,1 ,2, 2 },
         { 0, 0 ,2 ,1 ,2, 1 },
         { 0, 0 ,0 ,0 ,0,0 }
     };
         
      System.out.print(board1[6][5]);
      assertEquals(7, logic.getX());
      assertEquals(6, logic.getY());
      
      assertEquals(Winner.NOT_FINISHED, checkGameIsFinished(board1));
    
    }
      public void testGameIsFinished2() {
     testInitializeGame();
     int[][] board1 = {
         { 0, 0, 1, 2, 1,1 },
         { 0, 0, 1, 1, 2,1 },
         { 0, 0, 0, 2, 1,2 },
         { 0, 0 ,1 ,2 ,2, 2 },
         { 0, 0 ,0 ,0 ,2, 1 },
         { 0, 0 ,0 ,0 ,0,0 },
         { 0, 0 ,0 ,0 ,0,0 }
     };
         
      System.out.print(board1[6][5]);
      assertEquals(7, logic.getX());
      assertEquals(6, logic.getY());
      
      assertEquals(Winner.NOT_FINISHED, logic.checkGameIsFinished(board1));
    
    }
     @Test
     public void testGameIsFinished3() {
     testInitializeGame();
     int[][] board1 = {
         { 0, 0, 1, 1, 1,1 },
         { 0, 0, 0, 0, 0,0 },
         { 0, 0, 0, 0, 0,2 },
         { 0, 0 ,0 ,0 ,0, 2 },
         { 0, 0 ,0 ,0 ,0, 2 },
         { 0, 0 ,0 ,0 ,0, 0 },
         { 0, 0 ,0 ,0 ,0,0 }
     };
         
      System.out.print(board1[6][5]);
      assertEquals(7, logic.getX());
      assertEquals(6, logic.getY());
      
      assertEquals(Winner.PLAYER1, logic.checkGameIsFinished(board1));
    
    }
        //change to public so I can test it
    public Winner checkGameIsFinished(int[][] board)
    {
        int x=7,y=6;
		int row;
		int column;
		int coinsMultiplied;

		// Horizontal check
		row = y - 1;
		coinsMultiplied = 0;
		while (row >= 0) {
			column = 0;
			while (column < x - 3) {
				coinsMultiplied = board[column][row] * board[column + 1][row]
						* board[column + 2][row] * board[column + 3][row];
				if (coinsMultiplied == 1)
					return Winner.PLAYER1;
				if (coinsMultiplied == 16)
					return Winner.PLAYER2;
				column++;
			}
			row--;
		}

		// Vertical check
		column = 0;
		coinsMultiplied = 0;
		while (column < x) {
			row = y - 1;
			while (row > 2) {
				coinsMultiplied = board[column][row] * board[column][row - 1]
						* board[column][row - 2] * board[column][row - 3];
				if (coinsMultiplied == 0)
					break;
				if (coinsMultiplied == 1)
					return Winner.PLAYER1;
				if (coinsMultiplied == 16)
					return Winner.PLAYER2;
				row--;
			}
			column++;
		}

		// Diagonal from bottom left
		coinsMultiplied = 0;
		for (row = 3; row < y; row++) {
			column = 0;
			while (column < x - 3) {
				coinsMultiplied = board[column][row]
						* board[column + 1][row - 1]
						* board[column + 2][row - 2]
						* board[column + 3][row - 3];

				// Uncomment and swap row and column for performance
				// improvement.
				// if (coinsMultiplied==0) break;
				if (coinsMultiplied == 1)
					return Winner.PLAYER1;
				if (coinsMultiplied == 16)
					return Winner.PLAYER2;

				column++;
			}

		}

		// the other diagonal
		coinsMultiplied = 0;
		// Bugs corrected in the for-loop, Jens
		for (row = 3; row < y; row++) {
			column = 3;
			while (column < x) {
				coinsMultiplied = board[column][row]
						* board[column - 1][row - 1]
						* board[column - 2][row - 2]
						* board[column - 3][row - 3];

				// Uncomment and swap row and column for performance
				// improvement.
				// if (coinsMultiplied==0) break;
				if (coinsMultiplied == 1)
					return Winner.PLAYER1;
				if (coinsMultiplied == 16)
					return Winner.PLAYER2;

				column++;
			}
		}
		
		// Bug corrected, Jens. Check for TIE
		for (column = 0; column < x; column++)
		{
			if (board[column][0] == 0) break;
			if (column == x-1) return Winner.TIE;
		}

		return Winner.NOT_FINISHED;
	}

  
}
